﻿using EFEntity;
using Iservices;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AutoMapper;

namespace Services
{
    public class engage_resumeServices : Iengage_resumeServices
    {
        private readonly MyDbContext myDBContext;
        private readonly IMapper mapper;

        public engage_resumeServices(MyDbContext myDBContext, IMapper mapper)
        {
            this.myDBContext = myDBContext;
            this.mapper = mapper;
        }

        public async Task<List<engage_resume>> FenYe(FenYe fy)
        {
            var data = myDBContext.engage_resume.OrderBy(e => e.res_id).ToList();
            fy.Rows = data.Count();
            fy.Pages = (fy.Rows - 1) / fy.PageSize + 1;
            List<Eengage_resume> list = new List<Eengage_resume>();
            await Task.Run(() =>
            {
                list = data.Skip((fy.CurrentPage - 1) * fy.PageSize)
                    .Take(fy.PageSize)
                    .ToList();
            });
            //把EO-DTO
            List<engage_resume> list2 = new List<engage_resume>();
            foreach (Eengage_resume item in list)
            {
                engage_resume t = new engage_resume()
                {
                    res_id = item.res_id,
                    human_name = item.human_name,
                    engage_type = item.engage_type,
                    human_address = item.human_address,
                    human_postcode = item.human_postcode,
                    human_major_kind_id = item.human_major_kind_id,
                    human_major_kind_name = item.human_major_kind_name,
                    human_major_id = item.human_major_id,
                    human_major_name = item.human_major_name,
                    human_telephone = item.human_telephone,
                    human_homephone = item.human_homephone,
                    human_mobilephone = item.human_mobilephone,
                    human_email = item.human_email,
                    human_hobby = item.human_hobby,
                    human_specility = item.human_specility,
                    human_sex = item.human_sex,
                    human_religion = item.human_religion,
                    human_party = item.human_party,
                    human_nationality = item.human_nationality,
                    human_race = item.human_race,
                    human_birthday = item.human_birthday,
                    human_age = item.human_age,
                    human_educated_degree = item.human_educated_degree,
                    human_educated_years = item.human_educated_years,
                    human_educated_major = item.human_educated_major,
                    human_college = item.human_college,
                    human_idcard = item.human_idcard,
                    human_birthplace = item.human_birthplace,
                    demand_salary_standard = item.demand_salary_standard,
                    human_history_records = item.human_history_records,
                    remark = item.remark,
                    recomandation = item.recomandation,
                    human_picture = item.human_picture,
                    attachment_name = item.attachment_name,
                    check_status = item.check_status,
                    register = item.register,
                    regist_time = item.regist_time,
                    checker = item.checker,
                    check_time = item.check_time,
                    interview_status = item.interview_status,
                    total_points = item.total_points,
                    test_amount = item.test_amount,
                    test_checker = item.test_checker,
                    test_check_time = item.test_check_time,
                    pass_register = item.pass_register,
                    pass_regist_time = item.pass_regist_time,
                    pass_checker = item.pass_checker,
                    pass_check_time = item.pass_check_time,
                    pass_check_status = item.pass_check_status,
                    pass_checkComment = item.pass_checkComment,
                    pass_passComment = item.pass_passComment,
                };
                list2.Add(t);
            }
            return list2;
        }

        public async Task<Dictionary<string, object>> FenYe_resume_Async(int currentpage, string where)//分页查询简历筛选
        {
            List<Eengage_resume> list = new List<Eengage_resume>();
            Dictionary<string, object> di = new Dictionary<string, object>();
            //调用分页存储过程
            SqlParameter[] ps = {
                new SqlParameter() {ParameterName="@pageSize",Value=3 },
                new SqlParameter() {ParameterName="@keyName",Value="res_id" },
                new SqlParameter() {ParameterName="@tabelName",Value="engage_resume" },
                new SqlParameter() {ParameterName="@where",Value=where },
                new SqlParameter() {ParameterName="@currentPage",Value=currentpage },
               // new SqlParameter() {ParameterName="@lie",Value="*" },
                new SqlParameter() {ParameterName="@rows",Direction= ParameterDirection.Output,SqlDbType= SqlDbType.Int },
                new SqlParameter() {ParameterName="@pages",Direction= ParameterDirection.Output,SqlDbType= SqlDbType.Int }
                };
            list = await myDBContext.engage_resume.FromSqlRaw
                ("exec proc_FenYe @pageSize, @keyName, @tabelName, @where, @currentPage, @rows output, @pages output", ps)
                     .ToListAsync();
            int rows = (int)ps[5].Value;
            int pages = (int)ps[6].Value;
            List<engage_resume> list2 = mapper.Map<List<engage_resume>>(list);

            di["list"] = list2;
            di["rows"] = rows;
            di["pages"] = pages;
            return di;
        }

        public async Task<List<engage_resume>> getOne_resume_Async(int id)//查单个面试表信息
        {
            List<Eengage_resume> item = new List<Eengage_resume>();
            await Task.Run(() => { item = (from e in myDBContext.engage_resume where e.res_id == id select e).ToList(); });
            List<engage_resume> t = mapper.Map<List<engage_resume>>(item);
            return t;
        }

        public async Task<int> update_resume_status_Async(int id, int status)//修改简历表的面试状态
        {
            int result = 0;
            await Task.Run(() => {
                string sql = $" update [dbo].[engage_resume] set interview_status = {status} where [res_id] = '{id}' ";
                result = SqlHelper.ZSG(sql, "engage_resumeServices");
            });
            return result;
        }


        public async Task<int> update_resume_Async(engage_resume t)//修改简历信息
        {
            int result;
            Eengage_resume t2 = myDBContext.engage_resume.Find(t.res_id);
            t2.pass_checkComment = t.pass_checkComment;
            t2.interview_status = t.interview_status;
            t2.check_time = t.check_time;
            t2.checker = t.checker;
            result = await myDBContext.SaveChangesAsync();
            return result;
        }

        public async Task<int> update_resume_Async(int id,int status,string cm)//修改面试筛选
        {
            int result = 0;
            await Task.Run(() => {
                string sql = $" update [dbo].[engage_resume] set interview_status = {status} , pass_checkComment = '{cm}' where [res_id] = '{id}' ";
                result = SqlHelper.ZSG(sql, "engage_resumeServices");
            });
            return result;
        }

        public async Task<int> update_resume_Async1(engage_resume t)//修改简历信息
        {
            int result;
            Eengage_resume t2 = myDBContext.engage_resume.Find(t.res_id);
            t2.pass_passComment = t.pass_passComment;
            t2.interview_status = t.interview_status;
            result = await myDBContext.SaveChangesAsync();
            return result;
        }

        public async Task<int> update_resume(int id, int status, string pass_checkComment)//修改面试筛选
        {
            int result = 0;
            await Task.Run(() => {
                string sql = $" update [dbo].[engage_resume] set interview_status = {status} , pass_checkComment = '{pass_checkComment}' where [res_id] = '{id}' ";
                result = SqlHelper.ZSG(sql, "engage_resumeServices");
            });
            return result;
        }

        public async Task<engage_resume> SelectID(int id)
        {
            Eengage_resume list = new Eengage_resume();
            await Task.Run(() =>
            {
                list = myDBContext.engage_resume.Where(e => e.res_id == id).AsNoTracking().FirstOrDefault();
            });
            engage_resume list2 = mapper.Map<engage_resume>(list);
            return list2;
        }
    }
}
